package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SMSUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import com.itheima.health.vo.SetmealVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;


@RestController
public class OrderController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    // order/sendCodeToMobile

    @GetMapping("order/sendCodeToMobile/{telephone}")
    public Result sendCodeToMobile(@PathVariable("telephone") String telephone) {
        try {
            // 生成4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4);
            // 存入redis
            redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER + telephone, code, 90, TimeUnit.SECONDS);
            // 放入消息队列
            Map map = new HashMap();
            map.put("telephone", telephone);
            map.put("code", code);
            rabbitTemplate.convertAndSend("health-queue", map);
            System.out.println("校验验证码： " + code);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }

    @PostMapping("order/add")
    public Result add(@RequestBody Map map) {
        try {
            String fromCode = (String) map.get("validateCode");
            String telephone = (String) map.get("telephone");

            String code = (String) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_ORDER + telephone);
            if (code == null) {
                // 验证码为空 说明验证码已经过期
                return new Result(false, MessageConstant.SEND_VALIDATECODE_TIMEOUT);
            } else {
                // 不为空的情况：
                // 验证码正确：
                if (code.equals(fromCode)) {
                    // 业务层的 代码 预约判断 抛出异常
                    Order order = orderService.add(map);
                    return new Result(true, MessageConstant.ADD_ORDER_SUCCESS,order);
                } else {
                    // 验证码 不正确 ：
                    return new Result(false, MessageConstant.VALIDATECODE_ERROR);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }

    }


    @GetMapping("order/findOrderInfoByorderId/{id}")
    public Result findOrderInfoByorderId(@PathVariable("id") Integer id) {
        try {
            System.out.println(id);
            Map map = orderService.findOrderInfoByorderId(id);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
